// This file is part of Visuals -- Javascript based visualization system. 
// Copyright 2005 Nullsoft, Inc.
// Copyright 2011 Scott Sibley

var _BLEND = function() {
	this.REPLACE = [1, "Replace"];
	this.ADD = [2, "Additive"];
	this.MAX = [3, "Maximum"];
	this.AVG = [4, "Average"];
	this.SUB12 = [5, "Subtractive (1 - 2)"];
	this.SUB21 = [6, "Subtractive (2 - 1)"];
	this.MUL = [7, "Multiplative"];
	this.ADJ = [8, "Adjustable (cc=blend ratio)"];
	this.XOR = [9, "Xor"];
	this.MIN = [10, "Minimum"];
};
var BLEND = new _BLEND();

var max = Math.max;
var min = Math.min;
var blendtable = new Array();

for(j = 0; j <= 255; j++) {
	blendtable[j] = new Array();
	for (i = 0; i <= 255; i++) {
		blendtable[j][i] = i / 255 * j;
	}
}

function BLEND_ADD( a,  b)
{
	var r,t;
	r=(a&0xff)+(b&0xff);
	t=min(r,0xff);
	r=(a&0xff00)+(b&0xff00);
	t|=min(r,0xff00);
	r=(a&0xff0000)+(b&0xff0000);
	t|=min(r,0xff0000);
	r=(a&0xff000000)+(b&0xff000000);
	return t|min(r,0xff000000);
	/*var r,t;
	r=(a&0xf)+(b&0xf);
	t=min(r,0xf);
	r=(a&0xf0)+(b&0xf0);
	t|=min(r,0xf0);
	r=(a&0xf00)+(b&0xf00);
	t|=min(r,0xf00);
	r=(a&0xf000)+(b&0xf000);
	return t|min(r,0xf000);
	*/
}

function BLEND_MAX( a,  b)
{
	var t;
	var _a=a&0xff;
	var _b=b&0xff;
	t=max(_a,_b);
	_a=a&0xff00; _b=b&0xff00;
	t|=max(_a,_b);
	_a=a&0xff0000; _b=b&0xff0000;
	t|=max(_a,_b);
	return t;
	/*var t;
	var _a=a&0xf;
	var _b=b&0xf;
	t=max(_a,_b);
	_a=a&0xf0; _b=b&0xf0;
	t|=max(_a,_b);
	_a=a&0xf00; _b=b&0xf00;
	t|=max(_a,_b);
	return t;
	*/
}

function BLEND_MIN( a,  b)
{
	var t;
	var _a=a&0xff;
	var _b=b&0xff;
	t=min(_a,_b);
	_a=a&0xff00; _b=b&0xff00;
	t|=min(_a,_b);
	_a=a&0xff0000; _b=b&0xff0000;
	t|=min(_a,_b);

	return t;
/*
	var t;
	var _a=a&0xf;
	var _b=b&0xf;
	t=min(_a,_b);
	_a=a&0xf0; _b=b&0xf0;
	t|=min(_a,_b);
	_a=a&0xf00; _b=b&0xf00;
	t|=min(_a,_b);
	return t;
*/
}

function BLEND_AVG( a,  b)
{
	return ((a>>1)&~((1<<7)|(1<<15)|(1<<23)))+((b>>1)&~((1<<7)|(1<<15)|(1<<23)));
	//return ((a>>1)&~((1<<3)|(1<<7)|(1<<11)))+((b>>1)&~((1<<3)|(1<<7)|(1<<11)));
}


function BLEND_SUB( a,  b)
{
	var r, t;
	r=(a&0xff)-(b&0xff);
	t=max(r,0);
	r=(a&0xff00)-(b&0xff00);
	t|=max(r,0);
	r=(a&0xff0000)-(b&0xff0000);
	t|=max(r,0);
	r=(a&0xff000000)-(b&0xff000000);
	return t|max(r,0);
/*
	var r,t;
	r=(a&0xf)-(b&0xf);
	t=max(r,0);
	r=(a&0xf0)-(b&0xf0);
	t|=max(r,0);
	r=(a&0xf00)-(b&0xf00);
	t|=max(r,0);
	r=(a&0xf000)-(b&0xf000);
	return t|max(r,0);
*/
}


function BLEND_ADJ(a,  b, v)
{
	var t;
	t=blendtable[a&0xFF][v]+blendtable[b&0xFF][0xFF-v];
	t|=(blendtable[(a&0xFF00)>>8][v]+blendtable[(b&0xFF00)>>8][0xFF-v])<<8;
	t|=(blendtable[(a&0xFF0000)>>16][v]+blendtable[(b&0xFF0000)>>16][0xFF-v])<<16;
	return t;
/*
	var t;
	t=blendtable[a&0xF][v]+blendtable[b&0xF][0xF-v];
	t|=(blendtable[(a&0xF0)>>4][v]+blendtable[(b&0xF0)>>4][0xF-v])<<4;
	t|=(blendtable[(a&0xF00)>>8][v]+blendtable[(b&0xF00)>>8][0xF-v])<<8;
	return t;
*/
}

function BLEND_MUL(a,  b)
{
	var t;
	t=blendtable[a&0xFF][b&0xFF];
	t|=blendtable[(a&0xFF00)>>8][(b&0xFF00)>>8]<<8;
	t|=blendtable[(a&0xFF0000)>>16][(b&0xFF0000)>>16]<<16;
	return t;
/*
	var t;
	t=blendtable[a&0xF][b&0xF];
	t|=blendtable[(a&0xF0)>>4][(b&0xF0)>>8]<<4;
	t|=blendtable[(a&0xF00)>>8][(b&0xF00)>>8]<<8;
	return t;
*/
}

function BLEND_LINE(lbmode, fb, n, color)
{
  switch (lbmode&0xff)
  {
    case 2: fb[n]=BLEND_ADD(fb[n], color); break;
    case 3: fb[n]=BLEND_MAX(fb[n], color); break;
    case 4: fb[n]=BLEND_AVG(fb[n], color); break;
    case 5: fb[n]=BLEND_SUB(fb[n], color); break;
    case 6: fb[n]=BLEND_SUB(color, fb[n]); break;
    case 7: fb[n]=BLEND_MUL(fb[n], color); break;
    case 8: fb[n]=BLEND_ADJ(fb[n], color, (lbmode>>8)&0xff); break;
    case 9: fb[n]=fb[n]^color; break;
    case 10: fb[n]=BLEND_MIN(fb[n], color); break;
    default: fb[n]=color; break; // Replace
  }
}

BLEND.blend = BLEND_LINE;

/**
function BLEND4(AvsGlobalProxy *obj,  *p1,  w, var xp, var yp)
{
  var t;
  unsigned char a1,a2,a3,a4;
  a1=blendtable[255-xp][255-yp];
  a2=blendtable[xp][255-yp];
  a3=blendtable[255-xp][yp];
  a4=blendtable[xp][yp];
  t=blendtable[p1[0]&0xff][a1]+blendtable[p1[1]&0xff][a2]+blendtable[p1[w]&0xff][a3]+blendtable[p1[w+1]&0xff][a4];
  t|=(blendtable[(p1[0]>>8)&0xff][a1]+blendtable[(p1[1]>>8)&0xff][a2]+blendtable[(p1[w]>>8)&0xff][a3]+blendtable[(p1[w+1]>>8)&0xff][a4])<<8;
  t|=(blendtable[(p1[0]>>16)&0xff][a1]+blendtable[(p1[1]>>16)&0xff][a2]+blendtable[(p1[w]>>16)&0xff][a3]+blendtable[(p1[w+1]>>16)&0xff][a4])<<16;
  return t;      
}


function BLEND4_16(AvsGlobalProxy *obj,  *p1,  w, var xp, var yp)
{
  var t;
  unsigned char a1,a2,a3,a4;
  xp=(xp>>8)&0xff;
  yp=(yp>>8)&0xff;
  a1=blendtable[255-xp][255-yp];
  a2=blendtable[xp][255-yp];
  a3=blendtable[255-xp][yp];
  a4=blendtable[xp][yp];
  t=blendtable[p1[0]&0xff][a1]+blendtable[p1[1]&0xff][a2]+blendtable[p1[w]&0xff][a3]+blendtable[p1[w+1]&0xff][a4];
  t|=(blendtable[(p1[0]>>8)&0xff][a1]+blendtable[(p1[1]>>8)&0xff][a2]+blendtable[(p1[w]>>8)&0xff][a3]+blendtable[(p1[w+1]>>8)&0xff][a4])<<8;
  t|=(blendtable[(p1[0]>>16)&0xff][a1]+blendtable[(p1[1]>>16)&0xff][a2]+blendtable[(p1[w]>>16)&0xff][a3]+blendtable[(p1[w+1]>>16)&0xff][a4])<<16;
  return t;      
}
**/

